package com.jianyun.jyzs.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.db.DatabaseHelper;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by mlli on 2017/10/25.
 */

public class EngineerDao {
    private Context context;
    private Dao<EngineeringResult.EngineerBean,Integer> dao;
    private DatabaseHelper helper;

    public EngineerDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            dao = helper.getDao(EngineeringResult.EngineerBean.class);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void insert(EngineeringResult.EngineerBean engineeringResult){
        try {
            RceLog.d("lml",engineeringResult.getTextStr()+"___"+engineeringResult.isDeleted());
            dao.createOrUpdate(engineeringResult);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<EngineeringResult.EngineerBean> getEngineer(String enterpriseCode){
        QueryBuilder<EngineeringResult.EngineerBean, Integer> builder = dao.queryBuilder().orderBy("dtLong",false);
        List<EngineeringResult.EngineerBean> query = new ArrayList<>();
        try {
            query = builder.where().eq("enterpriseCode",enterpriseCode).and().ne("isDeleted",true).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }
    public EngineeringResult.EngineerBean getEngineerByid(String id,String enterpriseCode){
        QueryBuilder<EngineeringResult.EngineerBean, Integer> builder = dao.queryBuilder();
        List<EngineeringResult.EngineerBean> query = new ArrayList<>();
        try {
            builder.where().eq("id",id).and().eq("enterpriseCode",enterpriseCode);
            query = builder.query();
            if (query.size()>0){
                return query.get(0);
            }else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void delEngineer(String id) {
        DeleteBuilder<EngineeringResult.EngineerBean, Integer> builder = dao.deleteBuilder();
        try {
            builder.where().eq("id",id);
            builder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delEngineerAll() {
        DeleteBuilder<EngineeringResult.EngineerBean, Integer> builder = dao.deleteBuilder();
        try {
            builder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<EngineeringResult.EngineerBean> getEngineerByTag(String projectname, String place) {
        QueryBuilder<EngineeringResult.EngineerBean, Integer> builder = dao.queryBuilder();
        List<EngineeringResult.EngineerBean> query = new ArrayList<>();
        try {
        if (projectname!=null)builder.where().eq("projectName",projectname);
        if (place!=null)builder.where().eq("place",place);
            query = builder.query();
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return query;
        }

    }

    public List<EngineeringResult.EngineerBean> getEngineerByUser(String userId, String enterpriseCode) {
        QueryBuilder<EngineeringResult.EngineerBean, Integer> builder = dao.queryBuilder().orderBy("dtLong",true);
        List<EngineeringResult.EngineerBean> query = new ArrayList<>();
        try {
            query = builder.where().eq("enterpriseCode",enterpriseCode).and().eq("userId",userId).query();
            query = builder.query();
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return query;
        }
    }

    //倒序
    public List<EngineeringResult.EngineerBean> getEngineerByUser2(String userId, String enterpriseCode) {
        QueryBuilder<EngineeringResult.EngineerBean, Integer> builder = dao.queryBuilder().orderBy("dtLong",false);
        List<EngineeringResult.EngineerBean> query = new ArrayList<>();
        try {
            query = builder.where().eq("enterpriseCode",enterpriseCode).and().eq("userId",userId).query();
            query = builder.query();
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return query;
        }
    }

    public int getEngineerCount(String userid) {
        QueryBuilder<EngineeringResult.EngineerBean, Integer> builder = dao.queryBuilder();
        List<EngineeringResult.EngineerBean> query = new ArrayList<>();
        try {
            query = builder.where().eq("userId",userid).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query.size();
    }
}
